layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统任务表管理初始化
     */
    var Task = {
        tableId: "taskTable",	//表格id
        condition: {
            condition: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Task.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {title: '执行表达式', field: 'cronExpression', visible: true, align: 'center', width: '10%'},
            {title: '更新时间', field: 'updateTime', visible: true, align: 'center', width: '12%'},
            {title: '执行类', field: 'classBean', visible: true, align: 'center', width: '28%'},
            {title: '任务状态', field: 'state',align:'center', width: '10%', templet:function (d) {
                var str = "禁用任务";
                if(d.state == 1){
                    str = "等待执行";
                }else if(d.state == 2){
                    str = "正在执行";
                }
                return str;
            }},
            {title: '任务描述', field: 'descript', visible: true, align: 'center', width: '23%'},
            {title: '任务分组', field: 'jobGroup', visible: true, align: 'center', width: '7%'},
            {title: '任务名', field: 'jobName', visible: true, align: 'center', width: '15%'},
            {align: 'center', toolbar: '#task-table-toolbar', title: '操作', minWidth: 200, fixed:'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Task.search = function () {
        var where = {searchWords: $("#searchWords").val()};
        table.reload(Task.tableId, {where: where});
    };

    /**
     * 系统任务表添加
     */
    Task.toAdd = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加任务',
            area: ['40%', '80%'],
            content: Lu.ctxPath + '/system/task/toAdd',
            end: function () {
                Lu.adminOpenEnd(admin, tableReload);
            }
        });
     };

    /**
     * 修改系统任务表
     */
    Task.toUpdate = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改任务',
            area: ['40%', '80%'],
            content: Lu.ctxPath + '/system/task/toUpdate?id=' + data.id,
            end: function () {
                Lu.adminOpenEnd(admin, tableReload);
            }
        });
     };

    /**
     * 点击删除
     */
    Task.onDelete = function (ids) {
        var operation = function () {
            var ajax = new $ax(Lu.ctxPath + "/system/task/delete", function (data) {
                Lu.success("删除成功!");
                table.reload(Task.tableId);
            }, function (data) {
                Lu.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("data", ids);
            ajax.start();
        };
        Lu.confirm("是否刪除?", operation);
    };

    Task.onRunNow = function (data) {
        var ajax = new $ax(Lu.ctxPath + "/system/task/runNow", function (data) {
            Lu.success("执行中，请稍后!");
        }, function (data) {
            Lu.error("执行失败!" + data.responseJSON.message + "!");
        });
        ajax.set("data", data);
        ajax.start();
    };

   // 渲染表格
   var tableResult = table.render({
        elem: '#' + Task.tableId,
        url: Lu.ctxPath + '/system/task/listPage',
        toolbar: '#taskTableToolbar',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        id: Task.tableId,
        cols: Task.initColumn()
   });
   
    var tableReload = () => {
       table.reload(Task.tableId);
    };

   // 搜索按钮点击事件
   $('#btnSearch').click(function () {
        Task.search();
   });

   //头工具栏事件
   table.on('toolbar('+Task.tableId+')', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'add':
                //打开添加页面
                Task.toAdd();
                break;
            case 'deleteBatch':
                var data = checkStatus.data;
                if(data == null || data.length == 0){
                  layer.msg("请至少选择一个!");
                }
                var ids = [];
                $.each(data, function (index, d) {
                    ids.push({id: d.id, jobGroup: d.jobGroup, jobName: d.jobName});
                });
                Task.onDelete(JSON.stringify(ids));
                break;
        };
   });

   // 工具条点击事件
   table.on('tool(' + Task.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'update') {
            Task.toUpdate(data);
        } else if (layEvent === 'del') {
            Task.onDelete(JSON.stringify([{id: data.id}]));
        } else if (layEvent === 'runNow') {
            Task.onRunNow(JSON.stringify({id: data.id, jobGroup: data.jobGroup, jobName: data.jobName}));
        }
    });

    form.on('switch(taskSwitch)', function(data){
        var id = data.value;
        var state = 0;
        if(data.elem.checked){
            state=1;
        }
        var ajax = new $ax(Lu.ctxPath + "/system/task/update", function (data) {
        }, function (data) {
            Lu.error("操作失败！" + data.responseJSON.message)
        });
        ajax.set({id : id, state : state});
        ajax.start();
    });

});